package com.zhaoyue.study.concurrency.queue.delayqueuedemo;

import java.util.Random;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2016/12/8
 * Time: 17:52
 * To change this template use File | Settings | File Templates.
 */
public class CheckFood implements Runnable {

    private DelayQueue<Food> delayQueue;
    public CheckFood(DelayQueue<Food> delayQueue){
        this.delayQueue = delayQueue;
    }

    @Override
    public void run() {
        System.out.println("开始检查");
        try{
            boolean flag = true;
            while (flag){
                Food food = delayQueue.take();//此处会阻塞
                System.out.println("食物名称:"+food.getFoodName()+"保存时间:"+food.getSaveTime());
                if(delayQueue.isEmpty()){
                    flag = false;
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("检查完毕");
        }

    }

    public static void main(String[] args) {

        DelayQueue<Food> queue=new DelayQueue<Food>();
        Random r = new Random();
        queue.add(new Food("A", 1));
        queue.add(new Food("B", 2));
        queue.add(new Food("C", 5));
        queue.add(new Food("D", 3));
        queue.add(new Food("E", 8));
        queue.add(new Food("F", 9));

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.execute(new CheckFood(queue));
        executorService.shutdown();
    }
}
